package com.campus.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Http {
	/*
	 * post请求 url url p 参数 encode 编码方式
	 */
	public static String post(String url, String p, String encode)
			throws MalformedURLException, IOException {

		URLConnection conn = new URL(url).openConnection();
		String s;
		conn.setRequestProperty("Origin", "http://218.195.32.11");
		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		conn.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

		// conn.setRequestProperty(
		// "Referer",
		// "http://218.195.32.11/(icq1h545n1xvg545qtz4fwro)/xxjsjy.aspx?xh=1205500121&xm=%B6%CE%B4%BA%D1%E0&gnmkdm=N121611");
		conn.setDoInput(true);
		conn.setDoOutput(true);

		if (p != null)
			conn.getOutputStream().write(p.getBytes());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream(), encode));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}

		return b.toString();

	}

public	static String get(String url,String encode) throws MalformedURLException, IOException {

		HttpURLConnection conn = (HttpURLConnection) new URL(url)
				.openConnection();
		conn.setRequestProperty("Referer", url);
		conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
		// conn.setRequestProperty("Referer",
		// "http://218.195.32.11/(yh0kxoaa40kpjamm4u0myp55)/xs_main.aspx?xh=1205500121");
		String s;
		System.out.println(conn.getResponseCode());
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream(), encode));
		StringBuffer b = new StringBuffer();
		while ((s = br.readLine()) != null) {
			b.append(s);
		}

		return b.toString();

	}
}
